package org.carwler;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class SimpleTest {

	public static void main(String[] args) throws ParseException, IOException {
		 CloseableHttpClient httpClient= HttpClients.createDefault();
	        //知乎的话题问答
	        HttpGet httpget = new HttpGet("https://www.zhihu.com/question/58846658");
	        CloseableHttpResponse response = httpClient.execute(httpget);
	        HttpEntity httpEntity= response.getEntity();
	        //通过HttpComponents抓取网页信息转换成字符串
	        String content = EntityUtils.toString(httpEntity, "utf-8");
	        System.out.println("content: "+content);
	        String regex="QuestionHeader-title\">(.*?)</h1>.*?CopyrightRichText-richText\">(.*?)</span>.*?发布于?(.*?)\">";
	        //正则表达式爬取标题和最热的一个文章作者和内容
	        Pattern pattern = Pattern.compile(regex);
	        Matcher macher = pattern.matcher(content);
	        while(macher.find()) {
	            System.out.println("话题标题:" + macher.group(1));
	            System.out.println("最热回答:"+macher.group(2));
	            System.out.println("发布时间:" + macher.group(3));
	        }
	}
	
}
